package com.zzk.audiovideodemo.textureView;

import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.view.TextureView;

import com.blankj.utilcode.util.LogUtils;

import java.io.IOException;
import java.util.List;

/**
 * @ProjectName: AudioVideoDemo
 * @Package: com.zzk.audiovideodemo.textureView
 * @ClassName: CameraPreview
 * @Description:
 * @Author: brilliantzhao
 * @CreateDate: 2021.3.20 16:02
 * @UpdateUser:
 * @UpdateDate: 2021.3.20 16:02
 * @UpdateRemark:
 * @Version: 1.0.0
 */
public class CameraPreview extends TextureView implements TextureView.SurfaceTextureListener {

    private Camera mCamera;

    public CameraPreview(Context context) {
        super(context);
    }

    /**
     * 获取相机对象
     *
     * @return
     */
    public Camera getCamera() {
        return mCamera;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        //相机初始化，用于判断当前TextureView是否可用
        LogUtils.i("CameraPreview->onSurfaceTextureAvailable");
        try {
            mCamera = Camera.open();
            mCamera.enableShutterSound(false);

            //设置预览方向，预览方向默认是横屏的，所以这里要调整一下预览方向
            mCamera.setDisplayOrientation(90);
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            //获取最适合的分辨率
            Camera.Size previewSize = getCameraSize(parameters.getSupportedPreviewSizes(), width, height);
            parameters.setPreviewSize(previewSize.width, previewSize.height);
            //获取最适合的分辨率
            Camera.Size pictureSize = getCameraSize(parameters.getSupportedPictureSizes(), width, height);
            parameters.setPictureSize(pictureSize.width, pictureSize.height);

            //设置拍照之后，本地图片格式
            parameters.setPictureFormat(ImageFormat.JPEG);
            //拍照后的图片文件旋转90度
            parameters.setRotation(90);
            mCamera.setParameters(parameters);
            //将相机的预览效果显示出来
            mCamera.setPreviewTexture(surface);

            //开启预览
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取最合适的Size
     *
     * @param sizeList
     * @param width
     * @param height
     * @return
     */
    private Camera.Size getCameraSize(List<Camera.Size> sizeList, int width, int height) {
        Camera.Size tempSize = null;
        float aspectRatio = height * 1.0f / width;//求出预期横宽比
        float offset = aspectRatio;//预期横宽比和实际横宽比误差

        for (Camera.Size size : sizeList) {
            if (size.width < height || size.height < width) {
                continue;
            }

            //误差最小值
            if (Math.abs(aspectRatio - size.width * 1.0f / size.height) < offset) {
                offset = Math.abs(aspectRatio - size.width * 1.0f / size.height);
                tempSize = size;
            }
        }

        if (tempSize == null) {
            tempSize = sizeList.get(0);
        }
        return tempSize;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        LogUtils.i("CameraPreview->onSurfaceTextureSizeChanged");
        //监听布局变化时执行
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        LogUtils.i("CameraPreview->onSurfaceTextureUpdated");
        //画布，一帧数据
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        LogUtils.i("CameraPreview->onSurfaceTextureDestroyed");
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
        return true;
    }

}
